package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.IEquipmentDao;
import com.pristine.domain.EquipmentEntity;
import com.pristine.service.wsdl.EquipmentVO;
import com.pristine.service.wsdl.IEquipmentService;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;


@Service("equipmentService")
@Transactional
public class EquipmentServiceImpl implements IEquipmentService{

	@Autowired
	private IEquipmentDao equipmentDao;
	
	@Override
	public Integer createEquipment(EquipmentVO equipmentVO){
		EquipmentEntity equipment = VOToEntityConverter.convert(equipmentVO);
		equipmentDao.add(equipment);
		return equipment.getEquipmentId();
	}
	
	@Override
	public Integer updateEquipment(EquipmentVO equipmentVO){
		EquipmentEntity equipment = VOToEntityConverter.convert(equipmentVO);
		equipmentDao.update(equipment);
		
		return equipment.getEquipmentId();
	}
	
	@Override
	public boolean deleteEquipment(Integer id){
		try{
			EquipmentEntity entity = equipmentDao.find(id);
			equipmentDao.remove(entity);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	@Override
	public EquipmentVO getEquipmentById(Integer id){
		return EntityToVOConverter.convert(equipmentDao.find(id));
	}
	
	@Override
	public List<EquipmentVO> getAllEquipment() {
		
		List<EquipmentEntity> list = equipmentDao.list();
		List<EquipmentVO> voList= new ArrayList<EquipmentVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(EquipmentEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}
	
}
